package cn.gene.timeyan.ums.webapi.ums.service.impl;

import cn.gene.timeyan.common.exception.TimeyanServiceException;
import cn.gene.timeyan.common.restful.ResponseCode;
import cn.gene.timeyan.pojo.ums.dto.UserRegistryDTO;
import cn.gene.timeyan.pojo.ums.model.Set;
import cn.gene.timeyan.pojo.ums.model.User;
import cn.gene.timeyan.pojo.ums.vo.UserVO;
import cn.gene.timeyan.ums.service.IUserService;
import cn.gene.timeyan.ums.webapi.ums.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户基本（常用）信息表 服务实现类*
 *
 * @author LWG
 * @date 2022/7/22 17:18
 * @since 1.0.0@gene.cn
 */
@Slf4j
@Service
@DubboService
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Set isWatermark(Long userId) {
        Set set= userMapper.isWatermark(userId);
        return set;
    }

    @Override
    public User selectById(Long userId) {
        User user=userMapper.selectUser(userId);
        return user;
    }
    @Override
    public User SuperSelectById(Long userId) {
        User user=userMapper.SuperSelectUser(userId);
        return user;
    }
    @Override
    public void updatePasswordById(Long userId,String password){
        userMapper.updatePasswordById(userId,password);
    }

    @Override
    public void updateHeader(Long userId,String imgURL){
        userMapper.updateHeader(userId,imgURL);
    }

    @Override
    public void collectMark(Long userId,String markLevel){
        userMapper.collectMark(userId,markLevel);
    }


    @Override
    public Long addnew(UserRegistryDTO userRegistryDTO, String verifCode) {
        //1.判断验证码
        String verifCodeR = "123";
        log.info("777777777777777777777777");
        log.info(userRegistryDTO.getEmail());
        log.info(userRegistryDTO.getPhone());
        log.info(userRegistryDTO.getPassword());
        log.info(verifCode);
        log.info("777777777777777777777777");
        if(!verifCodeR.equals(verifCode)){
            log.info("11111111111111111111111111111111111");
            throw new TimeyanServiceException(ResponseCode.ERR_VERIFICATION_CODE_VALUE,"验证码错误");
        }
        //2.判断是否被注册过
        //不能都为空
        if(userRegistryDTO.getPhone()==null&&userRegistryDTO.getEmail()==null){
            log.info("222222222222222222222222222222222222");
            throw new TimeyanServiceException(ResponseCode.ERR_VERIFICATION_CODE_VALUE,"注册账号不可为空");
        }
        if(userRegistryDTO.getPhone()!=null&&!userRegistryDTO.getPhone().equals("")){
            //手机号
            String phone = userRegistryDTO.getPhone();
            //调用userMapper.getByPhone()方法查询
            User used = userMapper.getByPhone(phone);
            if(used!=null){
                log.info("22222222222222222222222222");
                //抛出异常
                throw new TimeyanServiceException(ResponseCode.ERR_CATEGORY_NAME_DUPLICATE,"改手机号已注册");
            }
        }else if(userRegistryDTO.getEmail()!=null&&!userRegistryDTO.getEmail().equals("")){
            log.info("333333333333333333333333");
            //邮箱号
            String email = userRegistryDTO.getEmail();
            //调用方法查询
            User user = userMapper.getByEmail(email);
            if(user!=null){
                throw  new TimeyanServiceException(ResponseCode.ERR_CATEGORY_NAME_DUPLICATE,"该邮箱号已被注册");
            }
        }else{
            throw new TimeyanServiceException(ResponseCode.ERR_VERIFICATION_CODE_VALUE,"输入不合法");
        }
        //3.插入对象
        //创建user对象
        User user = new User();
        //赋值
        BeanUtils.copyProperties(userRegistryDTO,user);
        user.setIsVip("0");
        user.setUserState(1);
        //调用usermapper.insert(user)插入数据
        int rows = userMapper.insert(user);
        if(rows!=1){
            throw new TimeyanServiceException(ResponseCode.ERR_NOTIME,"服务器忙,稍后再试");
        }
        return user.getUserId();
    }

    @Override
    public List<UserVO> list() {

        return userMapper.list();
    }
}
